package com.example.team.dao.Imp;

import com.example.team.dao.IVehicleUsageDao;
import com.example.team.entity.VehicleUsage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *根据接口操作数据库表的接口规范，对其进行实现
 * 编写者：
 * 时间：
 */
@Repository
public class IVehicleUsageDaoImpl implements IVehicleUsageDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertVehicleUsage(VehicleUsage vehicleUsage) {
        return jdbcTemplate.update(
                "insert into vehicle_usage(" +
                        "usageId," +
                        "vehicleId," +
                        "applicant," +
                        "departureLocation," +
                        "destination," +
                        "reason," +
                        "approve" +
                        ") " +
                        "values(?,?,?,?,?,?,?)"
                ,vehicleUsage.getUsageId()
                ,vehicleUsage.getVehicleId()
                ,vehicleUsage.getApplicant()
                ,vehicleUsage.getDepartureLocation()
                ,vehicleUsage.getDestination()
                ,vehicleUsage.getReason()
                ,vehicleUsage.getApprove()
        );
    }

    @Override
    public int deleteVehicleUsage(VehicleUsage vehicleUsage) {
        return jdbcTemplate.update(
                "delete from vehicle_usage " +
                "where usageId=?",vehicleUsage.getUsageId());
    }

    @Override
    public int updateVehicleUsage(VehicleUsage vehicleUsage) {
        //如果更新的字段为空，则保留旧数据
        VehicleUsage getVU=getVehicleUsageById(vehicleUsage);
        if(vehicleUsage.getVehicleId()==null)
            vehicleUsage.setVehicleId(getVU.getVehicleId());
        if(vehicleUsage.getApplicant()==null)
            vehicleUsage.setApplicant(getVU.getApplicant());
        if(vehicleUsage.getDepartureLocation()==null)
            vehicleUsage.setDepartureLocation(getVU.getDepartureLocation());
        if(vehicleUsage.getDestination()==null)
            vehicleUsage.setDestination(getVU.getDestination());
        if(vehicleUsage.getReason()==null)
            vehicleUsage.setReason(getVU.getReason());
        if(vehicleUsage.getApprove()==null)
            vehicleUsage.setApprove(getVU.getApprove());
        return jdbcTemplate.update("update vehicle_usage set " +
                        "" +
                        "vehicleId=?," +
                        "applicant=?," +
                        "departureLocation=?," +
                        "destination=?," +
                        "reason=?," +
                        "approve=? where usageId=?"
                ,vehicleUsage.getVehicleId()
                ,vehicleUsage.getApplicant()
                ,vehicleUsage.getDepartureLocation()
                ,vehicleUsage.getDestination()
                ,vehicleUsage.getReason()
                ,vehicleUsage.getApprove()
                ,vehicleUsage.getUsageId()
        );
    }

    @Override
    public List<VehicleUsage> getAllVehicleUsage(VehicleUsage vehicleUsage) {
        if(vehicleUsage.getApplicant()!=null)
            return jdbcTemplate.query("select " +
                            "usageId," +
                            "vehicleId," +
                            "applicant," +
                            "departureLocation," +
                            "destination," +
                            "reason," +
                            "approve " +
                "from vehicle_usage where applicant='"+vehicleUsage.getApplicant()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(VehicleUsage.class));
        else
            return jdbcTemplate.query("select " +
                            "usageId," +
                            "vehicleId," +
                            "applicant," +
                            "departureLocation," +
                            "destination," +
                            "reason," +
                            "approve " +
                "from vehicle_usage",new Object[]{},
                new BeanPropertyRowMapper<>(VehicleUsage.class));
    }

    @Override
    public VehicleUsage getVehicleUsage(VehicleUsage vehicleUsage) {
        List<VehicleUsage> result=jdbcTemplate.query("select " +
                        "usageId," +
                        "vehicleId," +
                        "applicant," +
                        "departureLocation," +
                        "destination," +
                        "reason," +
                        "approve " +
                        "from vehicle_usage where applicant='"+vehicleUsage.getApplicant()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(VehicleUsage.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }

    @Override
    public VehicleUsage getVehicleUsageById(VehicleUsage vehicleUsage) {
        List<VehicleUsage> result=jdbcTemplate.query("select " +
                        "usageId," +
                        "vehicleId," +
                        "applicant," +
                        "departureLocation," +
                        "destination," +
                        "reason," +
                        "approve " +
                        "from vehicle_usage where usageId='"+vehicleUsage.getUsageId()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(VehicleUsage.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
